﻿using UnityEngine;
using System.Collections;
using Spine.Unity;
using Spine.Unity.Modules;
using System;
using System.Collections.Generic;

public class ZoneFactory
{
    public ZoneFactory(ZoneRender renderer)
    {
        //InitLayer(renderer);
    }
    #region 对象

    public UnitObject AddDecoObject(nUnitData ud)
    {
        UnitObject uo = null;

        if (ud._res_name.Contains("lingxuan"))
        {
            uo = new RoleUnitObject(ud, ZoneCfg.CameraWorldLayer);
        }
        else
        {
            uo = new UnitObject(ud, ZoneCfg.CameraWorldLayer);
        }

        uo.Load();
        uo._Object.tag = "Zone";

        if (ud._order_block_4_groud == null)
            GetFitPos(uo);
        else
            ZoneManager._DecoLayer.BlockFresh.SetDone(ud._order_block_4_groud);


        return uo;
    }

    public UnitObject AddBuidingObject(nUnitData ud)
    {
        UnitObject uo = new UnitObject(ud, ZoneCfg.CameraWorldLayer);
        uo.Load();
        uo._Object.tag = "Zone";

        //  定位合适的位置
        if (ud._order_block_4_groud == null)
            GetFitPos(uo);

        return uo;
    }

    public UnitObject AddFoundationObject(nUnitData ud)
    {
        UnitObject uo = new UnitObject(ud, ZoneCfg.CameraWorldLayer);
        uo.Load();
        uo._Object.tag = "Zone";

        return uo;
    }

    public UnitObject AddObject(nUnitData ud)
    {
        switch(ud._UnitType)
        {
            case nUnitType.Role:
                return AddDecoObject(ud);
            case nUnitType.Building:
            case nUnitType.Decoration:
            case nUnitType.Foundation:
            case nUnitType.Triggers:
            case nUnitType.Wall:
            default:
                return AddFoundationObject(ud);
        }
    }

    #endregion

    #region 私有方法
    public Vector3 GetFitPos(UnitObject uo)
    {
        //  定位合适的位置
        Vector3 pos = uo.GetUnitRhombusLeft4World();
        int order = ZoneManager._CurrentLayer.BlockFresh.GetClosePointOrder(pos, uo._VBOdata);
        if (ZoneManager._CurrentLayer.BlockFresh.IsCanDone(order, uo._VBOdata)) 
        {
            Vector3 donepos = ZoneManager._CurrentLayer.BlockFresh.GetClosePointRhombus(order, uo._VBOdata);
            uo.SetUnitRhombusLeft4World(donepos, order);
        }
        else
            Debug.LogError("GetFitPos " + uo._EditorName);

        return pos;

    }
    #endregion

    #region 层级

    public nLayerData NewLayerData()
    {
        nLayerData data = new nLayerData();
        data._is_Collider = true;
        data._layer_type = nLayerType.Rectangle;
        data._Width = ((LayerObject)ZoneManager._Groud[0])._layerData._Width;
        data._Height = ((LayerObject)ZoneManager._Groud[0])._layerData._Height;
        data._layer_cell_l = ((LayerObject)ZoneManager._Groud[0])._layerData._layer_cell_l;
        data._layer_cell_h = ((LayerObject)ZoneManager._Groud[0])._layerData._layer_cell_h;

        return data;
    }

    Dictionary<string, UnitLayerObject> dtErrors = new Dictionary<string, UnitLayerObject>();

    /// <summary>
    /// 建立所有层级
    /// </summary>
    /// <param name="renderer"></param>
    public void InitLayer(ZoneRender renderer)
    {
        string strKey = string.Empty;
        string strValue = string.Empty;
        foreach (nLayerOrder intError in Enum.GetValues(typeof(nLayerOrder)))
        {
            strKey = intError.ToString();
            strValue = Enum.GetName(typeof(nLayerOrder), intError);

            UnitLayerObject layer = new UnitLayerObject(renderer, intError);
            dtErrors.Add(strKey, layer);
        }
    }

    /// <summary>
    /// 获取对应层级
    /// </summary>
    /// <param name="lo"></param>
    /// <returns></returns>
    public UnitLayerObject GetLayer(nLayerOrder lo)
    {
        if (dtErrors.ContainsKey(lo.ToString()))
            return dtErrors[lo.ToString()];

        return null;
    }

    public UnitLayerObject AddLayer(nLayerOrder lo)
    {
        UnitLayerObject layer = new UnitLayerObject(ZoneManager.Instance._ZoneRender, lo);

        dtErrors[lo.ToString()] = layer;

        return GetLayer(lo);
    }

    /// <summary>
    /// 判断2个层级的碰撞干扰关系
    /// </summary>
    /// <param name="lt1"></param>
    /// <param name="lt2"></param>
    public bool GetCollider(nUnitType type)
    {
        if (type == nUnitType.Building || 
            type == nUnitType.Foundation || 
            type == nUnitType.Wall )
            return false;

        return true;
    }
    #endregion
}
